# -*- coding: utf-8 -*-
import json
import click
from dgwt.utils import run_task
from concurrent.futures import ProcessPoolExecutor, wait


def echo(filename, content):
    print(filename)


@click.command()
@click.option('--tasks')
def main(tasks):
    with open(tasks) as f:
        tasks = json.load(f)["data"]
    with ProcessPoolExecutor(max_workers=5) as executor:
        future_tasks = []
        for task in tasks:
            name, url = task['name'], task['url']
            future_tasks.append(executor.submit(run_task, name, url, echo))
        wait(future_tasks)


if __name__ == '__main__':
    main()
